<?php
include("Design.php");
ShowHeader();
menu();

//if (!empty($_GET)&&!empty($_GET['height'])&&!$_GET['weight']){

if (isset($_GET['Pridej'])){
		$err = array();
	unset($err);
	    	
	if (!is_numeric($_GET['height']) || $_GET['height']<=0){
		$err['height'] = 'Špatná výška.';
	}
	    	
	if (!is_numeric($_GET['weight']) || $_GET['weight']<=0){
		$err['weight'] = 'Špatná váha.';
	}
}

$showForm = false;


if (isset($err)){
	if (!empty($err)){
		$showForm = true;
	
		foreach ($err as $current){
			echo "<div class='WrongValue'>" . $current . "</div><br/>";
		}
	}
		
}

$spojeni= mysql_connect("localhost",  "havelvoj",  "shaus1be" );
	if (!$spojeni) { 
				die(' Pripojení k databázi neúspešné: ' . mysql_error()); 
	}
		mysql_query("SET CHARACTER SET utf8;");
		mysql_query("SET NAMES utf8");
		mysql_select_db("havelvoj");

if(isset($_GET['weight'])&&(!showform)){
$bmi = $_GET['weight'] /($_GET['height'] * $_GET['height']/10000);

$query = "INSERT INTO U6Bmi (Date, Bmi)
			VALUES ('".date('Y-m-d')."','".$bmi."')";
$rs = mysql_query($query);
if (!$rs) {
		    echo "Chyba v 1. query: $query";
			return false;
		   }
		  

}


if ((!empty($_GET['year']))&&(!empty($_GET['month']))&&(!empty($_GET['day']))){
	$err2 = array();
	
	    	
	if (!is_numeric($_GET['month']) || $_GET['month']>12){
		$err2['month'] = 'Špatně zadaný měsíc.';
	}
	    	
	if (!is_numeric($_GET['year'])){
		$err2['year'] = 'Špatně zadaný rok.';
	}
	
	if($_GET['year']>2037){
		$err2['year'] = 'Bohužel funguje jen do roku 2037.
		<br>Pro další info:<a href="http://en.wikipedia.org/wiki/Year_2038_problem">Year 2038 Problem</a>';
	}
	    
	if (!is_numeric($_GET['day']) || $_GET['day']<=0|| $_GET['day']>31){
		$err2['day'] = 'Špatně zadaný den.';
	}
	
}




if (isset($err2)){
	if (!empty($err2)){
		
	
		foreach ($err2 as $current){
			echo "<div class='WrongValue'>" . $current . "</div><br/>";
		}
	}else{
   	$SetDate = $_GET['year']."-".$_GET['month']."-".$_GET['day'];
	$SetDate= strtotime($SetDate);
    $SetDate= date('Y-m-d',$SetDate);
    //echo $SetDate;

	}
	
}


?>
<form action="Overview.php" method="get">
		
	<br/><br/>
	<label for="day">Den</label>
	<input type="text" id="day" name="day" value="<?php echo $_GET['day'];?>">
	<br/><br/>
	<label for="month">Měsíc </label>
	<input type="text" id="month" name="month" value="<?php echo $_GET['month'];?>">
    <br/><br/>
	<label for="year">Rok</label>
	<input type="text" id="year" name="year" value="<?php echo $_GET['year'];?>">
	<br/><br/>
	<input type="submit" value="Zobraz">
</form>	
	   
<?php


if(!empty($SetDate)){
$query ="SELECT Date,Bmi FROM U6Bmi WHERE Date > '$SetDate'";
}
else{
$query ="SELECT Date,Bmi FROM U6Bmi";}			
			
$rs = mysql_query($query);
if (!$rs) {
		    echo "Chyba v 3. query: $query";
		    return false;
			}	
mysql_close();
echo "<table><tr><th>Datum</th><th>BMI</th></tr>";
	
			
while ($current=mysql_fetch_array($rs)){ 
			echo "<tr>";
			echo "<td>";
			echo $current["Date"];
			echo "</td>";
			echo "<td>";
			echo number_format($current["Bmi"],2);
			echo "</td>";
			echo "</tr>";			
		}
		
		echo "</table>";
 ShowFooter();
?>




</body>
</html>